package com.usts2022.classcostrecord;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.usts2022.classcostrecord.Utils.HttpUtils;
import com.usts2022.classcostrecord.entity.Report;
import com.usts2022.classcostrecord.entity.User;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ReportActivity extends AppCompatActivity {
    private ImageView photo,shop;
    private RadioGroup ReportRb;
    private FragmentManager fm;
    private Report mReport;
    private User mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
        //绑定
        fm = getSupportFragmentManager();
        photo = findViewById(R.id.photo);
        shop = findViewById(R.id.shop);
        ReportRb =findViewById(R.id.rb_report);
        //获取参数
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        mReport = (Report) extras.getSerializable("report");
        mUser = (User) extras.getSerializable("user");
        //ui
        uiUpdate(mReport);

        //改变事件
        ReportRb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                for (int i = 0; i < ReportRb.getChildCount(); i++) {
                    RadioButton rb = (RadioButton) radioGroup.getChildAt(i);
                    if (rb.isChecked()) {
                        changeRb(i);
                        break;
                    }
                }
            }
        });



    }



    //获取图片信息
    private void uiUpdate(Report report) {
        String itemPhoto = report.getItemPhoto();
        String shoppingReceipts = report.getShoppingReceipts();
        new Thread() {
            @Override
            public void run() {
                try {
                    Looper.prepare();
                    byte[] itemPhotoBt = HttpUtils.getImg(itemPhoto);
                    byte[] shoppingReceiptsBt = HttpUtils.getImg(shoppingReceipts);
                    ReportActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //使用BitmapFactory工厂，把字节数组转化为bitmap
                            Bitmap bitmap = BitmapFactory.decodeByteArray(itemPhotoBt, 0, itemPhotoBt.length);
                            //通过imageview，设置图片
                            photo.setImageBitmap(bitmap);
                            bitmap = BitmapFactory.decodeByteArray(shoppingReceiptsBt, 0, shoppingReceiptsBt.length);
                            //通过imageview，设置图片
                            shop.setImageBitmap(bitmap);
                            //
                            MsgFragment msgFragment = MsgFragment.newInstance(mReport,mUser);
                            fm.beginTransaction().replace(R.id.constraintLayout3, msgFragment).commitAllowingStateLoss();
                        }
                    });
                    Looper.loop();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }


    //登录注册切换按钮
    private void changeRb ( int i){
        switch (i) {
            case 0:
                MsgFragment msgFragment = MsgFragment.newInstance(mReport,mUser);
                fm.beginTransaction().replace(R.id.constraintLayout3, msgFragment).commitAllowingStateLoss();
                break;
            case 1:
                QuestionFragment questionFragment = QuestionFragment.newInstance(mUser,mReport);
                fm.beginTransaction().replace(R.id.constraintLayout3, questionFragment).commitAllowingStateLoss();
                break;
        }
    }

}